<?php

function fetchUrl($url,$options=array()) {
	$ch = curl_init($url);
	$defaultOption = array(
			CURLOPT_HEADER => 0,
			CURLOPT_TIMEOUT => 10,
			CURLOPT_RETURNTRANSFER=>1,
	);
	$options = $defaultOption+$options;
	foreach ($options as $k=>$v){
		curl_setopt($ch,$k,$v);
	}
	$res = curl_exec($ch); // Run it!
	if (curl_errno($ch)) {
		echo curl_error($ch),"\n";
		curl_close($ch);
		return false;
	}else {
		curl_close($ch);
		return $res;
	}
}

while (1){
	$urlPre = 'http://visitveb.duapp.com/index.php';
// 	$urlPre = 'http://127.0.0.1/myworks/visitweb/1/index.php';
	$html = fetchUrl("{$urlPre}?s=Index/getIp4check");
	if ($html) {
		$json = json_decode($html);
		if ($json) {
			$agentType	=	array(
					'HTTP'	=>	CURLPROXY_HTTP,
					'SOCKS4'=>	CURLPROXY_SOCKS4,
					'SOCKS5'=>	CURLPROXY_SOCKS5,
			);
			$options = array(
				CURLOPT_HTTPPROXYTUNNEL	=>	true,
				CURLOPT_PROXYAUTH=>CURLAUTH_BASIC,
				CURLOPT_PROXY=>	$json->agt_ip,
				CURLOPT_PROXYPORT=>intval($json->agt_port),
					//代理模式
				CURLOPT_PROXYTYPE=>$agentType[$json->agt_type],
				CURLOPT_HTTPHEADER=>	array(
						'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
						'Connection: keep-alive',
// 						"Cache-Control: max-age=0",
						'Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3',
				),
				CURLOPT_ENCODING=> 'gzip,deflate',
				CURLOPT_USERAGENT=>'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)',
			);
			$html = fetchUrl('http://kojiki.server.ne.jp/etc/pj235.cgi',$options);
			
			
			$respUrl = "$urlPre?s=Index/setRank/agt_id/{$json->agt_id}";
			if ($html) {
				$str = '#<FONT color="yellow">(\d)(\??)</FONT>#';
				if (preg_match($str, $html,$matches)) {
					fetchUrl("$respUrl/agt_rank/{$matches[1]}");
					echo "{$matches[1]}\n";
				}
			}else {
				fetchUrl("$respUrl/agt_err/1");
			}
		}
	}
}